package com.gmzg.gmzgoffice;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.gmzg.gmzgoffice.contacts.ContactsFragment;
import com.gmzg.gmzgoffice.me.MeFragment;
import com.gmzg.gmzgoffice.message.MessageFragment;
import com.gmzg.gmzgoffice.report.ReportFragment;
import com.gmzg.gmzgoffice.works.WorksFragment;
import com.hyphenate.chat.EMClient;
import com.readystatesoftware.systembartint.SystemBarTintManager;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private ContactsFragment mContactsFragment;
    private MeFragment mMeFragment;
    private MessageFragment mMessageFragment;
    private ReportFragment mReportFragment;
    private WorksFragment mWorksFragment;
    // 定义一个变量，来标识是否退出
    private static boolean isExit = false;

    private static Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };
    @Override
    public void onBackPressed() {
        if (!isExit){
            isExit = true;
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            mHandler.sendEmptyMessageDelayed(889,2000);
        }else{
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //注意设置状态栏要写在setContentView(R.layout.activity_main);之前
        //设置状态栏为透明的
        final int sdk = Build.VERSION.SDK_INT;
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();

        if (sdk >= Build.VERSION_CODES.KITKAT) {
//            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            // 设置透明状态栏
//            if ((params.flags & bits) == 0) {
//                params.flags |= bits;
//                window.setAttributes(params);
//
//            }
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.blue);
        }

        setContentView(R.layout.activity_main);

        //管理Fragment,检查Fragment是否存在
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ts = manager.beginTransaction();
        Fragment f = manager.findFragmentByTag("message");
        //为RadioGroup设置监听事件
        RadioGroup tabBar = (RadioGroup) findViewById(R.id.tab_bar);
        if (tabBar != null) {
            tabBar.check(R.id.rbtn_works);
            tabBar.setOnCheckedChangeListener(this);
        }


        if (f == null) {
            mMessageFragment = new MessageFragment();

        } else {
            mMessageFragment = (MessageFragment) f;

        }
        f = manager.findFragmentByTag("report");
        if (f == null) {
            mReportFragment = new ReportFragment();
        } else {
            mReportFragment = (ReportFragment) f;
        }
        f = manager.findFragmentByTag("works");
        if (f == null) {
            mWorksFragment = new WorksFragment();
            ts.replace(R.id.fragment_container, mWorksFragment, "works");
            ts.commit();
        } else {
            mWorksFragment = (WorksFragment) f;
            ts.replace(R.id.fragment_container, mWorksFragment, "works");
            ts.commit();
        }
        f = manager.findFragmentByTag("contacts");
        if (f == null) {
            mContactsFragment = new ContactsFragment();
        } else {
            mContactsFragment = (ContactsFragment) f;
        }
        f = manager.findFragmentByTag("me");
        if (f == null) {
            mMeFragment = new MeFragment();
        } else {
            mMeFragment = (MeFragment) f;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //开启一个事务
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ts = manager.beginTransaction();
        switch (checkedId) {
            case R.id.rbtn_message:
                ts.replace(R.id.fragment_container, mMessageFragment, "message");
                break;
            case R.id.rbtn_report:
                ts.replace(R.id.fragment_container, mReportFragment, "report");
                break;
            case R.id.rbtn_works:
                ts.replace(R.id.fragment_container, mWorksFragment, "works");
                break;
            case R.id.rbtn_contacts:
                ts.replace(R.id.fragment_container, mContactsFragment, "contacts");
                break;
            case R.id.rbtn_me:
                ts.replace(R.id.fragment_container, mMeFragment, "me");
                break;
        }
        //提交事务
        ts.commit();
    }

    @Override
    protected void onDestroy() {
        EMClient.getInstance().logout(true);
        Log.d("ddddddddd","用户退出登陆");
        //异步
//        EMClient.getInstance().logout(true, new EMCallBack() {
//            @Override
//            public void onSuccess() {
//
//            }
//
//            @Override
//            public void onError(int i, String s) {
//
//            }
//
//            @Override
//            public void onProgress(int i, String s) {
//
//            }
//        });
        super.onDestroy();
    }
}
